package com.zenitera.bigdata.transform;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * KeyedStream的每一个支流做聚合。执行完成后，会将聚合的结果合成一个流返回，所以结果都是DataStream
 * 如果流中存储的是POJO或者scala的样例类, 参数使用字段名.  如果流中存储的是元组, 参数就是位置(基于0...)
 * KeyedStream -> SingleOutputStreamOperator
 */
public class Flink12_TransForm_SumMinMax_01 {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<Integer> streamSource = env.fromElements(1, 2, 3, 4, 5, 6, 7, 8);
        KeyedStream<Integer, String> keyedStream = streamSource.keyBy(ele -> ele % 2 == 0 ? "偶数" : "奇数");
        keyedStream.sum(0).print("sum");
        keyedStream.max(0).print("max");
        keyedStream.min(0).print("min");

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
